<template>
  <div class="auditor-dialog-wrap">
    <el-dialog
      v-loading="loading"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :show-close="true"
      :append-to-body="true"
      :fullscreen="false"
      class="global-common-dialog auditor-dialog medium-common-dialog-size"
      @close="closeDialog"
      @open="openDialog"
      @opened="opened"
    >
      <Title :title="title" font-size="16px" style="margin: 0 0 24px"/>
      <div v-if="!isShowAll" class="list-tab-relative" >
        <div class="list-tab-absolute">
          <ListTab v-model="tabIndex" :tabs="dynamicTabs" :is-center="true" padding-left="0px"/>
        </div>
      </div>
      <el-form v-show="tabIndex === 0 && !isShowAll" :inline="true" :model="queryForm" label-position="left" @submit.native.prevent>
        <el-form-item label="输入姓名" label-width="70px" style="margin-bottom: 10px">
          <el-input v-model="queryForm.name" clearable size="small" placeholder="请输入内容" class="input-width-select" @keyup.enter.native="queryClick" />
        </el-form-item>
        <div class="query-button">
          <el-button
            class="search-btn-style"
            type="primary"
            size="small"
            style="margin-right: 6px"
            plain
            @click="queryClick"
          >查询</el-button>
          <clear-btn @click="queryEmpty" />
        </div>
      </el-form>
      <div v-loading="userListLoading" v-show="tabIndex === 0 && !isShowAll" class="total-table-list">
        <!-- v-if="userList.items.length > 0" -->
        <el-table
          ref="resourceTable"
          :data="userList.items"
          max-height="438px"
          height="438px"
          class="table-fixed"
          element-loading-text="加载中..."
          empty-text="暂无数据"
          header-cell-class-name="order-data-header-cell"
          hightlight-current-row
        >
          <el-table-column label="工号">
            <template slot-scope="scope">
              {{ scope.row.number || '--' }}
            </template>
          </el-table-column>
          <el-table-column label="运营人/对接人">
            <template slot-scope="scope">
              <div class="staff-info">
                <el-avatar :src="scope.row.avatar" :size="24" @error="errorHandler">
                  <img :src="adminIcon">
                </el-avatar>
                <span>{{ scope.row.userName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="right" width="50px">
            <template slot-scope="scope">
              <span v-if="selectsIds.indexOf(scope.row.id) >= 0" class="cancel-btn" @click="cancel(scope.row.id)">取消</span>
              <span v-else class="select-btn" @click="select(scope.row)">选择</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-area">
          <el-pagination
            v-if="userList.items.length > 0"
            :current-page="userList.page"
            :page-size="userList.perPage"
            :total="userList.total"
            :pager-count="5"
            :page-sizes="[20, 50, 100, 250, 500]"
            layout="total, prev, pager, next, sizes,jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <div v-if="userList.items.length === 0" style="position: relative; top: -64px; z-index: 2">
          <empty-placeholder />
        </div>
      </div>
      <div v-show="tabIndex === 1 || isShowAll" class="total-table-list">
        <el-table
          v-if="selectUser.length > 0"
          ref="resourceTable"
          :data="selectUser"
          max-height="438px"
          class="table-fixed"
          element-loading-text="加载中..."
          empty-text="暂无数据"
          hightlight-current-row
        >
          <el-table-column label="工号">
            <template slot-scope="scope">
              {{ scope.row.number || '--' }}
            </template>
          </el-table-column>
          <el-table-column label="运营人/对接人">
            <template slot-scope="scope">
              <div class="staff-info">
                <el-avatar :src="scope.row.avatar" :size="24" @error="errorHandler">
                  <img :src="adminIcon">
                </el-avatar>
                <span>{{ scope.row.userName }}</span>
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" align="right" width="50px">
            <template slot-scope="scope">
              <span v-if="selectsIds.indexOf(scope.row.id) >= 0" class="cancel-btn" @click="cancel(scope.row.id)">取消</span>
              <span v-else class="select-btn" @click="select(scope.row)">选择</span>
            </template>
          </el-table-column> -->
        </el-table>
      </div>
      <div v-if="(tabIndex === 1 || isShowAll )&& selectUser.length === 0">
        <empty-placeholder />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getAuditorList } from '@/api/legal';
import Title from '@/components/Title';
import EmptyPlaceholder from '@/components/EmptyPlaceholder';
import ClearBtn from '@/components/Common/ClearBtn';
import ListTab from '@/components/ListTab';
import adminIcon from '@/assets/user-avatar.png';
export default {
  name: 'AuditorDialog',
  components: { Title, EmptyPlaceholder, ClearBtn, ListTab },
  data() {
    return {
      adminIcon,
      loading: false,
      userListLoading: false,
      dialogVisible: false,
      isShowAll: false,
      tabIndex: 0,
      perPage: 20,
      page: 1,
      title: '',
      tabs: [{
        'id': 0,
        'name': '用户列表',
        'num': 0
      }, {
        'id': 1,
        'name': '当前选中',
        'num': 0
      }],
      type: 1, // 审批人类型 1: 审批人 ， 2:抄送人
      queryForm: {
        examineRoleId: '',
        name: ''
      },
      userList: {
        items: [],
        page: 1,
        perPage: 20,
        total: 0,
        isInint: true
      },
      selectUser: [],
      tempData: null
    };
  },
  computed: {
    selectsIds() {
      const ids = this.selectUser.map(item => item.id);
      return ids;
    },
    dynamicTabs() {
      return [{
        'id': 0,
        'name': '用户列表',
        'num': 0
      }, {
        'id': 1,
        'name': '当前选中',
        'num': this.selectsIds.length
      }];
    }
  },
  methods: {
    select(item) {
      this.selectUser.push(item);
      const data = {
        tempData: this.tempData,
        selectUser: this.selectUser
      };
      this.$emit('userSelect', data);
    },
    cancel(id) {
      const index = this.selectsIds.indexOf(id);
      this.selectUser.splice(index, 1);
      const data = {
        tempData: this.tempData,
        selectUser: this.selectUser
      };
      this.$emit('userSelect', data);
    },
    errorHandler() {
      return true;
    },
    queryEmpty() {
      this.queryForm.name = '';
      this.getList();
    },
    queryClick() {
      this.getList(true);
    },
    getList(reset = false) {
      if (reset) this.page = 1;
      this.userListLoading = true;
      const params = { ...this.queryForm, page: this.page, perPage: this.perPage };
      getAuditorList(params).then(res => {
        if (res.code === 200) {
          this.userList = res.data;
        }
        this.userListLoading = false;
      },
      result => {
        this.userListLoading = false;
      });
    },
    handleSizeChange(val) {
      this.perPage = val;
      this.getList(true);
    },
    handleCurrentChange(toPage) {
      this.page = toPage;
      this.getList();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    closeDialog() {
      const obj = this.queryForm;
      Object.keys(obj).forEach(i => { obj[i] = ''; });
      this.tabs[1].num = 0;
      this.selectUser = [];
      this.tabIndex = 0;
      this.type = 1;
      this.page = 1;
      this.perPage = 20;
      this.tempData = null;
      this.isShowAll = false;
      this.title = '';
      this.dialogVisible = false;
    },
    openDialog() {
    },
    opened() {
      this.getList(true);
    },
    initRoleData(params) {
      this.queryForm.examineRoleId = params.examineRoleId;
      this.selectUser = params.aduitors;
      this.tempData = params;
      this.type = 1;
      this.title = '添加审批人';
    },
    initSenderData(senders) {
      this.queryForm.examineRoleId = '';
      this.selectUser = senders;
      this.type = 2;
      this.title = '添加抄送人';
    },
    initShowAll(presons) {
      this.selectUser = presons;
      this.isShowAll = true;
      this.title = '查看全部';
    }
  }
};
</script>
<style lang="scss" scoped>
.auditor-dialog {
  & ::v-deep {
    .el-dialog {
      height: 556px;
    }
  }
  .list-tab-relative {
    position: relative;
    height: 18px;
    .list-tab-absolute {
      position: absolute;
      top: -46px;
      left: 50%;
      transform: translateX(-50%);
    }
  }
  .staff-info {
    display: flex;
    align-items: center;
    span {
      margin-left: 8px;
    }
  }
  .pagination-area {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 14px;
    .el-pagination {
      display: flex;
      justify-content: center;
    }
    & ::v-deep {
      .el-input__inner {
        width: 100%;
      }
    }
  }
  .select-btn {
    cursor: pointer;
    color: #406EFF;
  }
  .cancel-btn {
    cursor: pointer;
    color: #999;
  }
}
</style>
